//
//  CZTabBarItem.swift
//  WHWeiBo01
//
//  Created by 炜张 on 16/7/12.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CZTabBarItem: UITabBarItem {
    
    override var badgeValue: String? {
        didSet{
            //直接去主窗口根视图控制器  根据tabbar的target属性去去拿到控制器
       let tabBarControler = valueForKey("_target") as? UITabBarController
            
           //去解析控制器 拿到badgeView
            for tabbarChild in tabBarControler!.tabBar.subviews{
                
                if tabbarChild.isKindOfClass(NSClassFromString("UITabBarButton")!){
                    
                    for tabbarButtonChild in tabbarChild.subviews{
                        
                        if tabbarButtonChild.isKindOfClass(NSClassFromString("_UIBadgeView")!){
                            
                            for badgeViewChild in tabbarButtonChild.subviews {
                                
                                if badgeViewChild.isKindOfClass(NSClassFromString("_UIBadgeBackground")!){
                                    //找到相对于的背景
                                    
                                    //成员变量
                                    var count:UInt32 = 0
                                   
                        let ivars = class_copyIvarList(NSClassFromString("_UIBadgeBackground"), &count)
                                    
                                    for i in 0..<count {
                                        //指定位置的成员
                                        let ivar = ivars[Int(i)]
                                        //获得成员名称
                                        let name = ivar_getName(ivar)
                                        let type = ivar_getTypeEncoding(ivar)
                                        //成员名字转成字符串
                                        let nameStr = String(CString: name, encoding: NSUTF8StringEncoding)
                                        let typeStr = String(CString: type, encoding: NSUTF8StringEncoding)
                                        print(nameStr)
                                        print(typeStr)
                                        
                                        
                                        if nameStr == "_image" {
                                            badgeViewChild.setValue(UIImage(named: "main_badge"), forKey: "_image")
                                        }
                                        
                                        
                                        
                                    }
                                    
                                    
                                    
                                }
                                
                            }
                            
                            
                            
                        }
                        
                        
                        
                        
                    }
                    
                    
                    
                }
                
                
            }
        }
    }
    
    
    
}











